<!DOCTYPE HTML>
<html>

<head>
    <title>sparks-drugs</title>
    <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta charset="utf-8">
    <style type="text/css">
        body {
            font-family: Monospace;
            background-color: #333;
            margin: 0px;
            overflow: hidden;
        }

        #container {
            width: 1003px;
            height: 610px;
            margin-top: 50px;
            margin-left: -501px;
            left: 50%;
            position: absolute;
        }

        #testCanvas {
            background: #000;
        }

        .shadow {
            box-shadow: 0 0 25px #000;
        }
    </style>
</head>

<body>
    <div id="container" class="shadow">
        <canvas id="testCanvas"></canvas>
    </div>
    <script src="../../lib/requestAnimationFrame.min.js"></script>
    <script src="../../lib/stats.min.js"></script>
    <script src="../../lib/TweenLite.min.js"></script>
    <script src="//localhost:3001/build/proton.js"></script>
    <script>
        var canvas;
        var context;
        var proton;
        var renderer;
        var emitter;
        var stats, scale;

        main();

        function main() {
            initCanvas();
            addStats();
            loadImage();
        }

        function initCanvas() {
            canvas = document.getElementById("testCanvas");
            canvas.width = 1003;
            canvas.height = 610;
            context = canvas.getContext('2d');
            context.globalCompositeOperation = "lighter";

            canvas.addEventListener('mousedown', mouseDownHandler, false);
        }

        function loadImage() {
            var image = new Image()
            image.onload = function (e) {
                createProton(e.target);
                tick();
            }
            image.src = 'image/particle.png';
        }

        function addStats() {
            stats = new Stats();
            stats.setMode(2);
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';
            document.getElementById('container').appendChild(stats.domElement);
        }

        function createProton(image) {
            proton = new Proton;
            emitter = new Proton.Emitter();
            emitter.rate = new Proton.Rate(new Proton.Span(5, 10), new Proton.Span(.05, .2));

            emitter.addInitialize(new Proton.Body(image));
            emitter.addInitialize(new Proton.Mass(1));
            emitter.addInitialize(new Proton.Life(2, 4));
            emitter.addInitialize(new Proton.V(new Proton.Span(0.5, 1.5), new Proton.Span(0, 360), 'polar'));

            emitter.addBehaviour(new Proton.Alpha(1, [.7, 1]));
            scale = new Proton.Scale(1, 0);
            emitter.addBehaviour(scale);
            emitter.addBehaviour(new Proton.Color('random', 'random', Infinity, Proton.easeInSine));

            emitter.p.x = canvas.width / 2;
            emitter.p.y = canvas.height / 2;
            emitter.emit();
            proton.addEmitter(emitter);

            renderer = new Proton.CanvasRenderer(canvas);
            proton.addRenderer(renderer);
        }

        function mouseDownHandler(e) {
            scale.reset(3, 0);

            setTimeout(function () {
                scale.reset(1, 0);
            }, 800);
        }

        function tick() {
            requestAnimationFrame(tick);

            stats.begin();
            proton.update();
            stats.end();
        }
    </script>
</body>

</html>